<template>
  <div>
    <!--搜索框-->
    <div class="pd-10">
      <el-input style="width: 200px" suffix-icon="el-icon-search" placeholder="请输入患者姓名" class="mr-5" v-model="nickname"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>
    <!--新增按钮-->
    <div class="mg-10">
      <el-button class="ml-5" type="primary" @click="handleAdd">
        <i class="el-icon-circle-plus-outline"></i>新增
      </el-button>
      <!--批量删除-->
      <el-popconfirm
          title="您确定批量删除这些数据吗？"
          @confirm="delBatch">
        <el-button class="ml-5" type="danger" slot="reference">
          <i class="el-icon-remove-outline"></i>批量删除
        </el-button>
      </el-popconfirm>
      <!--导入-->
      <el-upload :action="action" style="display: inline-block"
                 :show-file-list="false" accept="xlsx" :on-success="handleExcelImportSuccess">
        <el-button class="ml-5" type="primary">
          <i class="el-icon-bottom"></i>导入
        </el-button>
      </el-upload>
      <el-button class="ml-5" type="primary" @click="exportFile">
        <i class="el-icon-top"></i>导出
      </el-button>
    </div>
    <el-table :data="tableData" border stripe :header-cell-style="{background: '#cccccc'}"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50">
      </el-table-column>
      <el-table-column prop="id" label="ID" width="50">
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="140">
      </el-table-column>
      <el-table-column prop="nickname" label="姓名" width="120">
      </el-table-column>
      <el-table-column prop="sex" label="性别" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.sex === 1">男</el-tag>
          <el-tag type="success" v-if="scope.row.sex === 0">女</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="age" label="年龄">
      </el-table-column>
      <el-table-column prop="email" label="邮箱">
      </el-table-column>
      <el-table-column prop="phone" label="电话">
      </el-table-column>
      <el-table-column prop="address" label="地址">
      </el-table-column>
      <el-table-column prop="role" label="角色">
        <template slot-scope="scope">
          <el-tag type="primary" v-if="scope.row.role === 'ROLE_DOCTOR'">医生</el-tag>
          <el-tag type="primary" v-if="scope.row.role === 'ROLE_PATIENT'">患者</el-tag>
          <el-tag type="success" v-if="scope.row.role === 'ROLE_ADMIN'">管理员</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="280px" v-if="this.user.role === 'ROLE_ADMIN'">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row)">编辑<i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="del(scope.row.id)">
            <el-button type="danger" slot="reference">删除<i class="el-icon-delete"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <!--新增弹出框-->
    <el-dialog title="医生信息" :visible.sync="dialogFormVisible" width="50%">
      <el-form :model="form" size="small">
        <el-form-item label="用户名" label-width="80px">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名" label-width="80px">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" label-width="80px">
          <el-select clearable v-model="form.sex" placeholder="请选择性别" style="width: 100%">
            <el-option v-for="item in sexs" :key="item.id" :label="item.name" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" label-width="80px">
          <el-input v-model="form.age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" label-width="80px">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" label-width="80px">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址" label-width="80px">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色" label-width="80px">
          <el-select clearable v-model="form.role" placeholder="请设置角色" style="width: 100%">
            <el-option v-for="item in roles" :key="item.name" :label="item.name" :value="item.flag">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {serverIp, serverPort} from "../../../public/config";

export default {
  name: "Patient",
  data() {
    return {
      tableData: [],
      nickname:"",
      deptId: "",
      form: {
        department: {}
      },
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      dialogFormVisible: false,
      multipleSelection: [],
      //分页
      pageNum: 1,
      pageSize: 5,
      total: 0,
      roles: [],
      departments: [],
      sexs: [{id:1,value: 0,name:'女'},{id:2,value: 1,name: '男'}],
      roleType: "ROLE_PATIENT",
      action: `http://${serverIp}:${serverPort}/patient/import`
    }
  },
  methods: {
    load() {
      this.request.get("/patient/page",{
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          nickname: this.nickname,
          roleType: this.roleType
        }
      }).then(res => {
        console.log(res);
        this.tableData = res.data.records
        this.total = res.data.total
      })
      //获取下拉角色信息
      this.request.get("/role").then(res => {
        this.roles = res.data.slice(2,3)
      })
    },
    exportFile() {
      window.open(`http://${serverIp}:${serverPort}/patient/export`)
    },
    handleExcelImportSuccess() {
      this.$message.success("上传成功")
      this.load()
    },
    handleAdd() {
      this.dialogFormVisible = true
    },
    save() {
      this.request.post("/patient/save",this.form).then(res => {
        console.log(this.form)
        console.log(res)
        if (res.code === '200') {
          this.dialogFormVisible = false
          this.form = {}
          this.$message.success("保存成功")
          this.load()
        }else {
          this.$message.error("保存失败")
        }
      })
    },
    handleEdit(row) {
      console.log("edit")
      console.log(row)
      this.form.department = Object.assign({},row.department)
      this.form = Object.assign({},row)
      this.dialogFormVisible = true
    },
    del(id) {
      this.request.delete("/patient/"+id).then(res => {
        if (res.code === '200') {
          this.$message.success("删除成功")
          this.load()
        }else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)
      this.request.post("/patient/del/batch",ids).then(res => {
        if (res.data) {
          this.$message.success("批量删除成功")
          this.load()
        }else {
          this.$message.error("批量删除失败")
        }
      })
    },
    reset() {
      this.nickname = ""
      this.deptId = ""
      this.load()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.load()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val
      this.load()
    }
  },
  created() {
    this.load()
  },
  watch: {
    dialogFormVisible() {
      if (this.dialogFormVisible === false) {
        this.form = {}
        this.form.department = {}
        console.log("执行了watch方法")
      }
    }
  }

}
</script>

<style>
.el-header {
  color: #333;
  line-height: 60px;
}

.el-aside {
  color: #333;
}
</style>